<template>
  <div class="enhanced-pressure-heatmap">
    <!-- 热图容器 -->
    <div class="heatmap-wrapper">
      <!-- X轴标签 -->
      <div class="x-axis-labels">
        <div class="x-label" v-for="x in 10" :key="x">X{{ x }}</div>
      </div>
      
      <!-- 热图主体 -->
      <div class="heatmap-main">
        <!-- Y轴标签 -->
        <div class="y-axis-labels">
          <div class="y-label" v-for="y in 10" :key="y">Y{{ y }}</div>
        </div>
        
        <!-- 热图网格 -->
        <div class="heatmap-grid">
          <div 
            v-for="(row, rowIndex) in data" 
            :key="rowIndex" 
            class="grid-row"
          >
            <div
              v-for="(cell, colIndex) in row"
              :key="colIndex"
              class="grid-cell"
              :class="[
                getPressureClass(cell.pressure),
                { 'show-grid': showGrid }
              ]"
              @click="handleCellClick(cell)"
              @mouseenter="handleCellHover(cell, $event)"
              @mouseleave="hideTooltip"
            >
              <!-- 数值显示 -->
              <span 
                v-if="showValues && shouldShowValue(cell)" 
                class="cell-value"
              >
                {{ cell.pressure }}
              </span>
              
              <!-- 坐标显示 -->
              <span 
                v-if="showCoordinates && shouldShowCoordinate(cell)" 
                class="cell-coordinate"
              >
                {{ cell.position }}
              </span>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 图例和控制 -->
      <div class="heatmap-legend">
        <div class="legend-gradient"></div>
        <div class="legend-labels">
          <span>0kPa</span>
          <span>4kPa</span>
          <span>8kPa</span>
        </div>
        
        <!-- 控制按钮 -->
        <div class="legend-controls">
          <button class="control-btn">
            <div class="btn-icon zoom-icon"></div>
            缩放
          </button>
          <button class="control-btn">
            <div class="btn-icon export-icon"></div>
            导出数据
          </button>
        </div>
      </div>
    </div>
    
    <!-- 悬浮提示 -->
    <div 
      v-if="tooltip.visible" 
      class="tooltip"
      :style="{ left: tooltip.x + 'px', top: tooltip.y + 'px' }"
    >
      <div class="tooltip-content">
        <div class="tooltip-title">{{ tooltip.data.position }}</div>
        <div class="tooltip-info">
          <span class="tooltip-label">压力值:</span>
          <span class="tooltip-value">{{ tooltip.data.pressure }} kPa</span>
        </div>
        <div class="tooltip-info">
          <span class="tooltip-label">状态:</span>
          <span class="tooltip-value" :class="tooltip.data.level">
            {{ getStatusText(tooltip.data.level) }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'EnhancedPressureHeatmap',
  props: {
    data: {
      type: Array,
      required: true
    },
    showGrid: {
      type: Boolean,
      default: true
    },
    showValues: {
      type: Boolean,
      default: true
    },
    showCoordinates: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      tooltip: {
        visible: false,
        x: 0,
        y: 0,
        data: {}
      }
    }
  },
  methods: {
    getPressureClass(pressure) {
      const value = parseFloat(pressure)
      if (value >= 6) return 'pressure-critical'
      if (value >= 4) return 'pressure-warning'
      return 'pressure-normal'
    },
    
    shouldShowValue(cell) {
      // 根据 miaoduo 设计，在特定位置显示数值
      const showPositions = [
        // Y2行的数值显示位置
        '2-6', '2-7', '2-8',
        // Y3行的数值显示位置
        '3-3', '3-5', '3-6', '3-7', '3-8',
        // Y4行的数值显示位置
        '4-2', '4-4', '4-5', '4-6', '4-7', '4-8', '4-10',
        // Y5行的数值显示位置
        '5-1', '5-2', '5-3', '5-4', '5-6', '5-7',
        // Y6行的数值显示位置
        '6-3', '6-4', '6-5', '6-6', '6-8',
        // Y8行的数值显示位置
        '8-2', '8-3', '8-4', '8-5', '8-6', '8-7',
        // Y9行的数值显示位置
        '9-4', '9-6'
      ]
      return showPositions.includes(`${cell.y}-${cell.x}`)
    },
    
    shouldShowCoordinate(cell) {
      // 只在边缘位置显示坐标
      return cell.x === 1 || cell.y === 1 || cell.x === 10 || cell.y === 10
    },
    
    getStatusText(level) {
      const statusMap = {
        'normal': '正常',
        'warning': '警告',
        'critical': '严重'
      }
      return statusMap[level] || '未知'
    },
    
    handleCellClick(cell) {
      this.$emit('cell-click', cell)
    },
    
    handleCellHover(cell, event) {
      this.tooltip = {
        visible: true,
        x: event.pageX + 10,
        y: event.pageY - 10,
        data: cell
      }
    },
    
    hideTooltip() {
      this.tooltip.visible = false
    }
  }
}
</script>

<style scoped>
.enhanced-pressure-heatmap {
  position: relative;
  background: #F8F8F8;
  border-radius: 6px;
  padding: 4px 16px 16px 4px;
}

.heatmap-wrapper {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* X轴标签 */
.x-axis-labels {
  display: flex;
  gap: 65px;
  margin-left: 36px;
  padding-left: 24px;
  padding-right: 24px;
}

.x-label {
  color: #6B7280;
  font-size: 12px;
  line-height: 16px;
  width: 14px;
  text-align: center;
}

/* 热图主体 */
.heatmap-main {
  display: flex;
  gap: 3px;
}

/* Y轴标签 */
.y-axis-labels {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 32px;
}

.y-label {
  color: #6B7280;
  font-size: 12px;
  line-height: 16px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 8px;
}

/* 热图网格 */
.heatmap-grid {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.grid-row {
  display: flex;
  gap: 1px;
}

.grid-cell {
  width: 71.7px;
  height: 18px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
  overflow: hidden;
}

.grid-cell.show-grid {
  border: 1px solid #E5E7EB;
}

.grid-cell:hover {
  transform: scale(1.1);
  z-index: 10;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 压力等级颜色 */
.pressure-normal {
  background: rgba(82, 196, 26, 0.60);
}

.pressure-warning {
  background: rgba(250, 173, 20, 0.75);
}

.pressure-critical {
  background: rgba(255, 77, 79, 0.90);
}

/* 单元格内容 */
.cell-value {
  color: #FFFFFF;
  font-weight: 500;
  font-size: 12px;
  line-height: 16px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.cell-coordinate {
  position: absolute;
  top: 2px;
  left: 2px;
  color: #6B7280;
  font-size: 10px;
  line-height: 12px;
}

/* 图例区域 */
.heatmap-legend {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  padding: 0 24px;
}

.legend-gradient {
  width: 150px;
  height: 12px;
  background: linear-gradient(90deg, #1677FF 0%, #FAAD14 50%, #FF4D4F 100%);
  border-radius: 6px;
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  width: 150px;
  margin-left: -150px;
  margin-top: 16px;
  font-size: 12px;
  color: #4B5563;
  line-height: 16px;
}

.legend-controls {
  display: flex;
  gap: 8px;
}

.control-btn {
  background: #FFFFFF;
  box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.05);
  border: 1px solid #D1D5DB;
  border-radius: 6px;
  padding: 5px 13px;
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  line-height: 16px;
  color: #000000;
  cursor: pointer;
  transition: all 0.2s;
}

.control-btn:hover {
  border-color: #1677FF;
  color: #1677FF;
}

.btn-icon {
  width: 12px;
  height: 12px;
  background-size: contain;
  background-repeat: no-repeat;
}

.zoom-icon {
  background-image: url('https://miaoduo.fbcontent.cn/private/resource/image/19918fa02f81743-4f066c7b-dc4d-427a-91bb-51e4e19e88fd.svg');
}

.export-icon {
  background-image: url('https://miaoduo.fbcontent.cn/private/resource/image/19918fa02f870f7-7408f6f7-dfb3-4ab1-aea5-bf420d1bdfdb.svg');
}

/* 悬浮提示 */
.tooltip {
  position: fixed;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.8);
  color: #FFFFFF;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 12px;
  line-height: 16px;
  pointer-events: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.tooltip-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.tooltip-title {
  font-weight: 600;
  margin-bottom: 4px;
}

.tooltip-info {
  display: flex;
  gap: 8px;
}

.tooltip-label {
  color: #D1D5DB;
}

.tooltip-value {
  font-weight: 500;
}

.tooltip-value.normal {
  color: #52C41A;
}

.tooltip-value.warning {
  color: #FAAD14;
}

.tooltip-value.critical {
  color: #FF4D4F;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .x-axis-labels {
    gap: 50px;
    margin-left: 28px;
  }
  
  .grid-cell {
    width: 60px;
    height: 16px;
  }
  
  .cell-value {
    font-size: 11px;
  }
}

@media (max-width: 768px) {
  .enhanced-pressure-heatmap {
    padding: 4px 8px 8px 4px;
  }
  
  .x-axis-labels {
    gap: 35px;
    margin-left: 20px;
  }
  
  .grid-cell {
    width: 45px;
    height: 14px;
  }
  
  .cell-value {
    font-size: 10px;
  }
  
  .legend-controls {
    flex-direction: column;
    gap: 4px;
  }
  
  .control-btn {
    padding: 4px 8px;
    font-size: 11px;
  }
}
</style>
